package com.pn.controller;


import com.pn.entity.Auth;
import com.pn.entity.LoginUser;
import com.pn.entity.User;
import com.pn.service.UserService;
import com.pn.utils.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginUser loginUser){
        //校验验证码：
        if(!stringRedisTemplate.hasKey(loginUser.getVerificationCode())){
            return Result.err(Result.CODE_ERR_BUSINESS,"输入的验证码错误！");
        }
        //根据用户名查询用户
        User user = userService.findUserByCode(loginUser.getUserCode());
        if(user != null){//查到了用户
            //查到的用户状态是已审核
            if(user.getUserState().equals(WarehouseConstants.USER_STATE_PASS)){
                //将用户录入的密码进行加密
                String password = DigestUtil.hmacSign(loginUser.getUserPwd());
                if (password.equals(user.getUserPwd())){
                    Map<String, Object> claims = new HashMap<>();
                    claims.put("userId",user.getUserId());
                    claims.put("userCode",user.getUserCode());

                    String token = JwtUtil.createJWT(claims);
                    stringRedisTemplate.opsForValue().set(token,"",2880000, TimeUnit.MILLISECONDS);
                    log.info("token为：{}",token);
                    return Result.ok("登录成功！",token);
                }else{//查到的用户的密码和用户录入的密码不同
                    return Result.err(Result.CODE_ERR_BUSINESS, "密码不正确！");
                }

            }else {//查到的用户状态是未审核
                return Result.err(Result.CODE_ERR_BUSINESS, "用户未审核！");
            }
        }else{//没有查到用户
            return Result.err(Result.CODE_ERR_BUSINESS, "该用户不存在！");
        }
    }

    //获取当前用户的消息
   @GetMapping("/curr-user")
    public Result currentUser(){
        Map<String,Object> map=(Map<String, Object>) ThreadLocalUtil.get();
        String code =(String) map.get("userCode");
        User user = userService.findUserByCode(code);
        return Result.ok(user);

    }

  /*  //加载用户权限下的菜单树
    @GetMapping("/user/auth-list")
    public Result loadAuthTree(){
        Map<String,Object> map=(Map<String, Object>) ThreadLocalUtil.get();
        Integer userId =(Integer) map.get("userId");
        List<Auth> list = userService.autoTreeByUid(userId);
        return Result.ok(list);
    }*/

    //退出登录
    @DeleteMapping("/logout")
    public Result logout(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        stringRedisTemplate.delete(token);
        return Result.ok("退出系统...");
    }
}
